package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageBean;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {
    @Autowired
    private IYddAdvimgService iYddAdvimgService;
    @Autowired
    private IYddAdvposService iYddAdvposService;



    @GetMapping("/list")
    public ResultData page(String advimgdesc,String advimgpos, int page, Long limit){
        Page<YddAdvimg> pageInfo=new Page<>(page,limit);
        LambdaQueryWrapper<YddAdvimg> lqw=new LambdaQueryWrapper<>();
        lqw.like(advimgdesc!=null,YddAdvimg::getAdvimgdesc,advimgdesc);
        lqw.like(advimgpos!=null,YddAdvimg::getAdvimgpos,advimgpos);
        lqw.orderByDesc(YddAdvimg::getId);
        iYddAdvimgService.page(pageInfo,lqw);
        List<YddAdvimgDto>yddAdvimgDtos=pageInfo.getRecords().stream().map(
                item->{
                    YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
                    BeanUtils.copyProperties(item,yddAdvimgDto);
                    //获取对象分类的名字,按名字去找有多少广告
                    LambdaQueryWrapper<YddAdvpos>lambdaQueryWrapper=new LambdaQueryWrapper<>();
                    //获取item对象的advimgpos(advpos的id)
                    lambdaQueryWrapper.eq(YddAdvpos::getId,item.getAdvimgpos());
                    //根据ID获取advpos对象
                    YddAdvpos yddAdvpos = iYddAdvposService.getOne(lambdaQueryWrapper);
                   yddAdvimgDto.setAdvposname(yddAdvpos.getAdvposname());
                   return yddAdvimgDto;
                }
        ).collect(Collectors.toList());
        PageBean<YddAdvimgDto> pageBean=new PageBean<>();
        pageBean.setData(yddAdvimgDtos);//封装的具体集合数据
        pageBean.setPageSize(pageInfo.getSize());//每页条数
        pageBean.setCount(pageInfo.getTotal());//总条数
        pageBean.setTotalPages(pageInfo.getPages());//总页数
        pageBean.setCurrentPage(pageInfo.getCurrent());//当前页


        return ResultData.ok("",pageBean);
    }

    @GetMapping("/rowInfo")
    public ResultData getById(@RequestParam Integer id){
        LambdaQueryWrapper<YddAdvimg>lqw=new LambdaQueryWrapper<>();
        lqw.eq(YddAdvimg::getId,id);
        YddAdvimg one = iYddAdvimgService.getOne(lqw);

        return ResultData.ok("",one);
    }

    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvimg yddAdvimg){
        LambdaUpdateWrapper<YddAdvimg> luw=new LambdaUpdateWrapper<>();
        luw.eq(YddAdvimg::getId,yddAdvimg.getId());
        iYddAdvimgService.update(yddAdvimg,luw);
        return ResultData.ok("",yddAdvimg);


    }

    //需要修改
    @GetMapping("/del")
    public ResultData deleteById(@RequestParam Integer id){
        LambdaQueryWrapper<YddAdvimg>lqw=new LambdaQueryWrapper<>();
        lqw.eq(YddAdvimg::getId,id);
        iYddAdvimgService.remove(lqw);
        return ResultData.ok("","删除成功");

    }

    @PostMapping("/add")
    public ResultData save(@RequestBody YddAdvimg yddAdvimg) {
        iYddAdvimgService.save(yddAdvimg);
        return ResultData.ok("",yddAdvimg);

    }
}
